<template>
  <div class="nav-header">
    <el-icon :size="30" style="cursor: pointer" @click="changeFold"
      ><component :is="isFold ? 'Expand' : 'Fold'"></component
    ></el-icon>
    <div class="header-content">
      <breadcrumb :breadcrumbs="breadcrumb" />
      <div class="header-user">
        <el-dropdown trigger="click">
          <span class="el-dropdown-link">
            {{ userInfo.name }}
            <el-icon class="el-icon--right">
              <arrow-down />
            </el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="goToPerson">个人中心</el-dropdown-item>
              <el-dropdown-item>退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, computed } from 'vue';
import { useRoute } from 'vue-router';
import { useVuex } from '@/store/index';

import { findMenuToBreadcrumb } from '@/utils/map-route';

import breadcrumb from './breadcrumb.vue';

export default defineComponent({
  components: {
    breadcrumb,
  },
  emits: ['foldChange'],
  setup(props, context) {
    // console.log(context);

    const store = useVuex();
    const userInfo = computed(() => store.state.LoginModule.userInfo);
    const isFold = ref(false);
    const changeFold = () => {
      isFold.value = !isFold.value;
      context.emit('foldChange', isFold.value);
    };

    const goToPerson = () => {
      console.log('个人中心');
    };

    const menus = computed(() => store.state.LoginModule.menus);
    const breadcrumb = computed(() => {
      const route = useRoute();
      return findMenuToBreadcrumb(menus.value, route.path);
    });
    // console.log(breadcrumb);

    return {
      userInfo,
      isFold,
      changeFold,
      goToPerson,
      breadcrumb,
    };
  },
});
</script>

<style scoped lang="less">
.nav-header {
  display: flex;
  width: 100%;
  .header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-left: 20px;
    flex: 1;
  }
}
</style>
